package com.jl15988.simpletools.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jl15988.simpletools.common.annotation.Log;
import com.jl15988.simpletools.common.constant.OperationType;
import com.jl15988.simpletools.common.utils.Result;
import com.jl15988.simpletools.system.domain.dto.MenuDTO;
import com.jl15988.simpletools.system.domain.dto.PasswordUpdateDTO;
import com.jl15988.simpletools.system.domain.dto.UserUpdateDTO;
import com.jl15988.simpletools.system.domain.entity.User;
import com.jl15988.simpletools.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author jl15988
 */
@Slf4j
@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/v1/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取当前用户信息")
    @GetMapping("/info")
    public Result<User> getCurrentUser() {
        User user = userService.getCurrentUser();
        return Result.success(user);
    }

    @Log(title = "更新用户信息", operationType = OperationType.UPDATE)
    @ApiOperation("更新用户信息")
    @PutMapping("/info")
    public Result<String> updateUserInfo(@Validated @RequestBody UserUpdateDTO updateDTO) {
        boolean success = userService.updateUserInfo(updateDTO);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Log(title = "修改密码", operationType = OperationType.UPDATE)
    @ApiOperation("修改密码")
    @PutMapping("/password")
    public Result<String> changePassword(@Validated @RequestBody PasswordUpdateDTO passwordDTO) {
        // 验证两次密码是否一致
        if (!passwordDTO.getNewPassword().equals(passwordDTO.getConfirmPassword())) {
            return Result.validateFailed("两次输入的密码不一致");
        }
        boolean success = userService.changePassword(passwordDTO.getOldPassword(), passwordDTO.getNewPassword());
        return success ? Result.success("修改成功") : Result.error("修改失败");
    }

    @ApiOperation("分页查询用户列表")
    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<IPage<User>> getUserPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("用户名") @RequestParam(required = false) String username,
            @ApiParam("角色") @RequestParam(required = false) Integer role) {
        IPage<User> pageResult = userService.getUserPage(page, size, username, role);
        return Result.success(pageResult);
    }

    @Log(title = "更新用户角色", operationType = OperationType.UPDATE)
    @ApiOperation("更新用户角色")
    @PutMapping("/{userId}/role/{role}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> updateUserRole(
            @ApiParam("用户ID") @PathVariable Long userId,
            @ApiParam("角色") @PathVariable Integer role) {
        boolean success = userService.updateUserRole(userId, role);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Log(title = "删除用户", operationType = OperationType.DELETE)
    @ApiOperation("删除用户")
    @DeleteMapping("/{userId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> deleteUser(@ApiParam("用户ID") @PathVariable Long userId) {
        boolean success = userService.deleteUser(userId);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    @ApiOperation("获取用户菜单")
    @GetMapping("/menus")
    public Result<List<MenuDTO>> getUserMenus() {
        return Result.success(userService.getUserMenus());
    }
} 